﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SnakeGame
{
    class Game
    {
        static readonly int MARGIN = 2;
        static readonly int WIDTH = 50;
        static readonly int HEIGHT = 30;

        Point content;
        GameOptions gameOptions;
        Point startPoint;
        Point food;
        Snake snake;
        bool go;
        ConsoleKeyInfo key;
        Controller control;
        ConsoleRenderer render;
        Field field;



        public Game()
        {
            content = new Point(WIDTH - MARGIN, HEIGHT - MARGIN);
            startPoint = new RandomPoint(content, MARGIN).RandPoint;
            gameOptions = new GameOptions(WIDTH, HEIGHT);
            food = new RandomPoint(content, MARGIN).RandPoint;
            snake = new Snake(startPoint);
            field = new Field(content);
            go = true;
            control = new Controller(snake, this);
            

        }

        public List<Point> Field
        {
            get { return field.Border; }
        }

        public bool Go
        {
            set { go = value; }
        }

        public char SignEncoding(int sign)
        {
            var a = Encoding.GetEncoding("IBM437");

            char[] c = a.GetString(new byte[] { (byte)sign }).ToCharArray();

            return c[0];
        }

        public void GameOver()
        {
            this.go = true;
            Console.SetCursorPosition(14, 9);
            Console.WriteLine("-- G A M E  O V E R --");
            Console.SetCursorPosition(17, 11);
            Console.WriteLine("Play again? (Y/n)");
            key = Console.ReadKey();
            control.GameControl(key);

            if (this.key.Key == ConsoleKey.N)
            {
                this.Go = false;
            }


            render.Clear();
            startPoint = new RandomPoint(this.content, MARGIN).RandPoint;
            snake = new Snake(startPoint);
            control = new Controller(snake, this);
            

        }

        public void GenerateFood()
        {
            if (snake.Growing(food))
            {
                this.food = new RandomPoint(this.content, MARGIN).RandPoint;
                while (snake.isIntersection(food))
                {
                    this.food = new RandomPoint(this.content, MARGIN).RandPoint;
                }
            }
        }

        public void Start()
        {
            while (go)
            {
                render = new ConsoleRenderer(   SignEncoding(177),
                                                SignEncoding(176),
                                                SignEncoding(1),
                                                SignEncoding(178), 
                                                this.food, this.snake, this.gameOptions
                                            );

                render.RenderField(field.Border);

                render.ClearSnake(this.snake, this.food);

                control.SnakeControl();
                snake.ChangePosition();

                this.GenerateFood();
                render.RenderFood();
                render.RenderSnake();

                if (snake.SelfEating() || snake.isIntersectionWithField(field.Border) )
                {
                    this.GameOver();
                }

                System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(0.2).TotalMilliseconds);
            }
        }
    }

}
